package com.jianzh5.service.impl;

import com.jianzh5.common.DataPage;
import com.jianzh5.common.DataTables;
import com.jianzh5.dao.BaseDaoI;
import com.jianzh5.entity.TAsk;
import com.jianzh5.service.AskServiceI;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by jianzh5 on 2015/7/16.
 */
@Service("askService")
public class AskServiceImpl implements AskServiceI{
          
    private final Logger logger = Logger.getLogger(AskServiceImpl.class);

    @Autowired
    private BaseDaoI<TAsk> askDao;

    @Override
    public DataTables dataTables(String order, String sort, DataPage dataPage) {
        String hql = "from TAsk t";
        String totalHql = "select count(*) from TAsk t";
        hql += " order by t."+sort+" "+order;
        List<TAsk> tlist = askDao.findByPosition(hql, dataPage.getStart(), dataPage.getLength());
        long records = askDao.count(totalHql);
        DataTables dt = new DataTables();
        dt.setDraw(dataPage.getDraw());
        dt.setRecordsTotal(records);
        dt.setRecordsFiltered(records);
        dt.setData(tlist);
        return dt;
    }

    @Override
    public void editAsk(TAsk tAsk) {
        if(tAsk.getId()== null || tAsk.getId() == 0){  //添加
            tAsk.setCreateDate(new Date());
            askDao.save(tAsk);
        }else{
            TAsk t = askDao.get(TAsk.class,tAsk.getId());
            BeanUtils.copyProperties(tAsk,t,new String[]{"createDate"});
        }
    }

    @Override public void delAsk(int aid) {
        TAsk t = askDao.get(TAsk.class,aid);
        System.out.println(t.toString());
        askDao.delete(t);
    }
}
